﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace TeamResourceManageAssist
{
    public partial class DatePart : UserControl
    {
        //DateTime _ChartStartDate;
        //DateTime _ChartEndDate;
        private string _test;
        public static DependencyProperty ChartStartDateProperty;
        public static DependencyProperty ChartEndDateProperty;
        public static DependencyProperty TestTestProperty;

        static DatePart()
        {
            ChartStartDateProperty = DependencyProperty.Register("ChartStartDate",
                typeof(DateTime), typeof(DatePart),
                new PropertyMetadata(new DateTime(), new PropertyChangedCallback(OnChartStartDatePropertyChanged)));

            ChartEndDateProperty = DependencyProperty.Register("ChartEndDate",
                typeof(DateTime), typeof(DatePart),
                new PropertyMetadata(new DateTime(), new PropertyChangedCallback(OnChartEndDatePropertyChanged)));

            TestTestProperty = DependencyProperty.Register("TestTest",
                typeof(string), typeof(DatePart), 
                new PropertyMetadata(null, new PropertyChangedCallback(OnTestPropertyChanged)));

        }

        static void OnTestPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            DatePart dp = target as DatePart;
            dp.TestTest = e.NewValue as string;
        }

        static void OnChartStartDatePropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            DatePart dp = target as DatePart;
            dp.ChartStartDate = (DateTime)e.NewValue;
            dp.RefreshCanvas();
        }

        static void OnChartEndDatePropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            DatePart dp = target as DatePart;
            dp.ChartEndDate = (DateTime)e.NewValue;
            dp.RefreshCanvas();
        }


        public DatePart()
        {
            // Required to initialize variables
            InitializeComponent();
            RefreshCanvas();
        }


        [TypeConverter(typeof(DateTimeTypeConverter))]
        public DateTime ChartStartDate
        {
            get {
                return (DateTime)GetValue(ChartStartDateProperty); 
                //return _ChartStartDate;
            }
            set
            {
                if (value.DayOfWeek != DayOfWeek.Sunday)
                {
                    value = value.AddDays(-Convert.ToDouble(value.DayOfWeek));
                }
                if (value.Date != ChartStartDate)
                {
                    SetValue(ChartStartDateProperty, value);
                    //_ChartStartDate = value;
                    RefreshCanvas();
                }
            }
        }

        [TypeConverter(typeof(DateTimeTypeConverter))]
        public DateTime ChartEndDate
        {
            get { 
                return (DateTime)GetValue(ChartEndDateProperty); 
                //return _ChartEndDate;
            }
            set
            {
                if (value.DayOfWeek != DayOfWeek.Saturday)
                {
                    value = value.AddDays(Convert.ToDouble(DayOfWeek.Saturday - value.DayOfWeek));
                }
                if (value.Date != ChartEndDate)
                {
                    SetValue(ChartEndDateProperty, value);
                    //_ChartEndDate = value;
                    RefreshCanvas();
                }
            }
        }

        public string TestTest
        {
            get { return (string)GetValue(TestTestProperty); }
            set {
                _test = value;
                SetValue(TestTestProperty, value); 
            }
        }

        /// <summary>
        /// 重新绘制
        /// </summary>
        void RefreshCanvas()
        {
            if (Panel != null)
            {
                ClearCanvas();
                DrawCanvas();
            }

        }

        /// <summary>
        /// 清空画布
        /// </summary>
        void ClearCanvas()
        {
            Panel.Children.Clear();
        }

        /// <summary>
        /// 绘制
        /// </summary>
        void DrawCanvas()
        {
            int n = ChartEndDate.Subtract(ChartStartDate).Days / 7 + 1;
            for (int i = 0; i < n; i++)
            {
                WeekPart weekPart = new WeekPart();
                weekPart.tx_Date.Text = ChartStartDate.AddDays(i * 7).ToLongDateString();
                Panel.Children.Add(weekPart);
            }
        }
    }
}